/*
 * @Author: ayun chengyunwu@jiatui.com
 * @Date: 2023-05-08 11:30:02
 * @LastEditors: ayun chengyunwu@jiatui.com
 * @LastEditTime: 2023-05-16 18:25:49
 * @FilePath: /ift-economy-h5/src/utils/entry.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// import JSEncrypt from 'jsencrypt'
import { JSEncrypt } from 'encryptlong'
/**
 * RSA公钥加密
 *
 * @param content 待加密数据
 * @param publicKey 公钥
 * @returns {string} 加密结果
 */
export function rsaEncrypt(content: any, publicKey: string) {
  const encrypt = new JSEncrypt({})
  encrypt.setPublicKey(publicKey)
  return encrypt.encrypt(content)
}

/**
 * RSA私钥解密
 *
 * @param content 待解密数据
 * @param privateKey 私钥
 * @returns {string} 解密结果
 */
export function rsaDecrypt(content: any, privateKey: string) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  return encryptor.decryptLong(content)
}


// 对长加密数据进行处理
export function getRsaDecrypt(content: any) {
  const privateKey: any = '-----BEGIN PRIVATE KEY-----MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOqfV0vuxE8gIhFy/7oJVeHLAn1BkQhzRY1WuQHtMMFjQa/7liMg0kf7zdCqMBzACDinXar1DHRnoI8JBCjA69TUYepShHb9GbCDcEad0RJiIz01NzyP/NiaJmS7ZASG2BXpZXqEvOYswjEdLXO+uxLhxTvAxqgdKOZstS00t3+pAgMBAAECgYBHvGxulRSEtOh1uVmjQhN2NCIQtiloEgakDm4GvsT3fCerbRscpn5k8AKf2ElZoQVifRnQmZgzRzP6R+FzrHjuRhqN4A5DTix6CIG0CIazKEalzzCIU5gxPrGDfGJr/613AtEKbNiG4oelL+l/pwSv3tJtocFpOF0uoN52KZPXmQJBAPa2V+lSvynprtjj8kucKHeUxnd5oXvLDUyUnuKHuhNTLqbB2YeCxc8PyRlwXXcnL2bARD4wl/LdYBUav5evJsMCQQDzdHu2o47VuEuZe7BAl6YKA0izFNGuQ1ziROlqgUWLSfKGYBsm8L8AYFAnu1rneW52DwC0zpiWWNMMCJ5QYNEjAkEA8NUJNjvPXnO+efzyXjq/ChSSNLjoGTca8WG3Q/0oeIrf5GshFPZkcH/BWHLm2lTHIe+X7nlzKuJNU+y7kK+rDwJBAPLhFMMi7U7ea92VfkMSh72ZhR/T91QpNDMwmMV+bkkF3wScQLpal9LiQ1VAHvKrPL5fPU8khSoJN5PbzUVXMY0CQAyZOAWy43/4LfywfIp1KRZq2v1pCeXjzihZreD+Lvf9nY4o6DFHHo6mACm2TG7jxioWL5TJ3e4BKgtwPVCwWvs=-----END PRIVATE KEY-----'
  try {
    const decryptData: any = rsaDecrypt(content, privateKey)
    const transcodingData: any = JSON.parse(decodeURIComponent(decryptData))
    return transcodingData;
  }
  catch (ex) {
    return false;
  }
}

/**
 * 判断是否位微信浏览器
 * @returns 
 */
export const isWeixinBrowser = () => {
  const ua = navigator.userAgent.toLowerCase()
  return /micromessenger/.test(ua)
}
